package handler;

import http.HttpMethod;
import http.MyHttpRequest;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * @author Karl
 * @Date 2023/1/4 - 14:18
 */
public class ConnectHandler implements Handler {
    //返回是否停止
    @Override
    public boolean handler(MyHttpRequest request, Socket from, Socket to) {
        byte[] buf = new byte[1024];
        int len = 0;
        try {
            InputStream stream = to.getInputStream();
            OutputStream os = from.getOutputStream();

            request.sendOrigin(to.getOutputStream(), from.getOutputStream());

            while ((len = stream.read(buf)) > 0) {
                os.write(buf, 0, len);
            }
        } catch (IOException e) {
            return true;
        }
        return true;
    }

    @Override
    public boolean isSupport(MyHttpRequest request,Socket remote) {
        return request.getMethod().equals(HttpMethod.CONNECT);
    }
}
